package com.choosefine.it.basedata.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.ProjectType;
import com.choosefine.it.basedata.model.dto.SelectProjectTypeBy;
import com.choosefine.it.basedata.model.vo.ProjectTypeVO;
import com.choosefine.it.basedata.service.ProjectTypeService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.utils.DateUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;


/**
 * @author 潘钱钦 上午11:12:24
 * 
 */
@RestController
@RequestMapping("/basedata/projectType")
@Api(value="工程类型相关",description="顾文伟")
public class ProjectTypeController extends BaseController{
	Logger logger = LoggerFactory.getLogger(TeamTypeController.class);
	@Autowired
	private ProjectTypeService projectTypeService;

	@ApiOperation(value = "根据id获取工程类型列表（多个id以“ , ”隔开）", notes = "")
	@ApiImplicitParam(name = "idss", value = "工程类型的id", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "/{idss}", method = RequestMethod.GET)
	public String get(@PathVariable String idss) {
		String result = null;
		List<Integer> ids = new ArrayList<>();
		// 单个id
		if (!StringUtils.isEmpty(idss) && idss.split(",").length == 1) {
			String id = idss;
			result = responseSuccess(projectTypeService.getProjectTypeById(Integer.valueOf(id)));
		} else {
			// 多个id，要以“,”隔开传入
			String[] id = idss.split(",");
			for (String i : id) {
				Integer n = Integer.valueOf(i);
				ids.add(n);
			}
			result = responseArraySuccess(projectTypeService.getProjectTypeList(ids));
		}
		return result;
	}
	
	
	/**
	 * 工程类型
	 * 
	 * @param user
	 * @param response
	 * @return
	 * @throws BusinessException 
	 * @throws Exception
	 */
	@ApiOperation(value = "存储工程类型", notes = "传入工程类型对象，存储相关工程类型信息")
	@ResponseBody
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.POST)
	public String save(@RequestBody ProjectType projecttype) throws BusinessException {
		String result = null;
		if (projecttype != null) {
			projecttype.setCtime(DateUtils.getNowTime());
			Integer num = projectTypeService.insert(projecttype);
			if (num.intValue()>0) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_CREATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 修改工程类型
	 * 
	 * @param user
	 * @param response
	 * @return
	 * @throws BusinessException 
	 * @throws Exception
	 */
	@ApiOperation(value = "修改工程类型", notes = "传入工程类型对象，根据ID更改相关工程类型基础信息")
	@ResponseBody
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "修改成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.PUT)
	public String update(@RequestBody ProjectType projecttype) throws BusinessException {
		String result = null;
		if (projecttype != null) {
			projecttype.setUtime(DateUtils.getNowTime());
			Integer num = projectTypeService.updateByPrimaryKeySelective(projecttype);
			if (num.intValue()>0) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	@ApiOperation(value = "删除工程类型", notes = "传入工程类型ID，根据ID删除相关工程类型基础信息")
	@ApiImplicitParam(name = "id", value = "工程类别ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		String result = null;
		if (id != null) {
			Integer num = projectTypeService.deleteByPrimaryKey(id);
			if (num.intValue()>0) {
				result = responseSuccess(num,ResultCode.SUCCESS,ResultCode.MSG_DELETE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_DELETE_FAILD);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	@ApiOperation(value = "获取工程类型列表")
	@RequestMapping(method = RequestMethod.GET, value = "/projecttypelist", produces = "application/json")
	public String getProjectTypeList() throws BusinessException {
		String result = null;
		List<ProjectType> projecttypelist = projectTypeService.findAllByPage();
		result = responseArraySuccess(projecttypelist);
		return result;
	}
	
	@ApiOperation(value = "获取工程类型列表不分页(毛俊)",response=ProjectTypeVO.class)
	@RequestMapping(method = RequestMethod.GET, value = "/getAllProjectTypeList", produces = "application/json")
	public String getAllProjectTypeList() throws BusinessException {
		String result = null;
		List<ProjectTypeVO> list=null;
		List<ProjectType> projecttypelist = projectTypeService.selectAllProjectType();
		if (projecttypelist != null) {
			list=new ArrayList<ProjectTypeVO>(projecttypelist.size());
			for (ProjectType projectType : projecttypelist) {
				ProjectTypeVO projectTypeVO=new ProjectTypeVO();
				projectTypeVO.setId(projectType.getId());
				projectTypeVO.setProjectType(projectType.getProjectType());
				list.add(projectTypeVO);
			}		
			result = responseArraySuccess(list);
		} else {
			result = responseFail(ResultCode.MSG_ERROR);
		}
		return result;
	}
	@ApiOperation(value = "工程类型排序")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "fromId", value = "移动项", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "toId", value = "移动到的位置", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "order", value = "移动标识（1上移-1下移）", dataType = "Integer", required = true, paramType = "path")
	})
	@RequestMapping(value = "/projectTypeOrder/{fromId}/{toId}/{order}", method = RequestMethod.PUT)
	public String projectTypeOrder(@PathVariable Integer fromId, @PathVariable Integer toId, @PathVariable Integer order){
		String result = null;
		int num = 0;
		if(order>0){
			num += projectTypeService.projectTypeOrderUp(fromId, toId);
		}else{
			num += projectTypeService.projectTypeOrderDown(fromId, toId);
		}
		if(num>0){
			result = responseSuccess(num);
		}else{
			result = responseFail();
		}
		return result;
	}
	@ApiOperation(value = "按条件查询工程类型")
	@RequestMapping(value = "/selectProjectTypeByName", method = RequestMethod.POST)
	public String selectProjectTypeByName(@RequestBody SelectProjectTypeBy selectProjectTypeBy){
		String result = null;
		Map<String,Object> map = new HashMap<>();
		if(null != selectProjectTypeBy.getName() && null != selectProjectTypeBy.getName().trim()){
			map.put("name", selectProjectTypeBy.getName());
		}
		if(null != selectProjectTypeBy.getIsValid()){
			map.put("isValid", selectProjectTypeBy.getIsValid());
		}
		PageInfo<ProjectType> projectType = projectTypeService.selectProjectTypeByName(map, selectProjectTypeBy.getPageNum(), selectProjectTypeBy.getPageSize());
		return responseArraySuccess(projectType);
	}
	/**
	 * add
	 * juchengyang 2017/2/28
	 */
	@ApiOperation(value = "APP获取工程类型列表（分页）")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "path"),
		@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "path")})
	@RequestMapping(value = "/getProjectTypeListAPP/{pageNum}/{pageSize}", method = RequestMethod.GET)
	public String getProjectTypeListAPP(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
		String result = null;
		PageInfo<ProjectType> projectTypeList = projectTypeService.selectProjectTypeList(pageSize,pageNum);
		result = responseArraySuccess(projectTypeList); 
		return result;
	}
}
